package com.yonyou.pmclouds.workconnect.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertMultiProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.template.MultiInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.messagenotice.entity.MessageNoticeVO;
import com.yonyou.pmclouds.messagenotice.rmiitf.MessageNoticeMaintain;
import com.yonyou.pmclouds.workConnect.entity.WorkConReceiverVO;
import com.yonyou.pmclouds.workConnect.service.rmiitf.WorkConReceiverMaintain;
import com.yonyou.pmclouds.workconnect.mapper.WorkConReceiverMapper;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author 唐焕
 * @date 2018/8/21
 */
@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = WorkConReceiverMaintain.class)
public class WorkConReceiverMaintainImpl implements WorkConReceiverMaintain {

    private static String[] UPDATE_FIELD = {WorkConReceiverVO.RECEIVE_STATUS, WorkConReceiverVO.RECEIVED_TIME,
            WorkConReceiverVO.SUGGESTION};

    @Autowired
    private WorkConReceiverMapper mapper;
    @Autowired
    private MessageNoticeMaintain noticeMaintain;

    @Override
    public WorkConReceiverVO add(WorkConReceiverVO vo) throws BusinessException {
        if (vo == null)
            return null;
        SingleInsertBpTemplate<WorkConReceiverVO> bpTemplate = new SingleInsertBpTemplate<WorkConReceiverVO>(mapper);
        // 添加审计信息
        bpTemplate.addBeforeProcessor(new AuditInfoInsertProcessor<WorkConReceiverVO>());

        return bpTemplate.insert(vo);
    }

    @Override
    public WorkConReceiverVO[] addVOs(WorkConReceiverVO[] vos) throws BusinessException {
        if (ArrayUtils.isEmpty(vos)) {
            return null;
        }
        MultiInsertBpTemplate<WorkConReceiverVO> bp = new MultiInsertBpTemplate<>(mapper);
        // 添加审计信息
        bp.addBeforeProcessor(new AuditInfoInsertMultiProcessor<WorkConReceiverVO>());
        return bp.insert(vos);
    }

    @Override
    public WorkConReceiverVO update(WorkConReceiverVO vo) throws BusinessException {
        if (vo == null)
            return null;
        SingleUpdateBpTemplate<WorkConReceiverVO> bpTemplate = new SingleUpdateBpTemplate<WorkConReceiverVO>(mapper, UPDATE_FIELD);
        vo = bpTemplate.update(vo);
        if(WorkConReceiverVO.RECEIVE_STATUS_ACCEPTED == vo.getReceiveStatus()) {
            // 接收人接收之后，则给制单人发送消息提醒
            MessageNoticeVO noticeVO = new MessageNoticeVO();
            noticeVO.setRelateKey(vo.getPkWorkconnect());
            noticeVO.setRelateType(BillTypeConst.WORKCONNECT_BILLTYPE);
            noticeVO.setCreator(vo.getPkReceiver());
            noticeVO.setCreatorName(vo.getReceiverName());
            noticeMaintain.insertMessageNotice(noticeVO);
        }
        return bpTemplate.update(vo);
    }

    @Override
    public String delete(String id, String changeFlag) throws BusinessException {
        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(changeFlag))
            return null;
        WorkConReceiverVO tempVO = new WorkConReceiverVO();
        tempVO.setPkWorkConReceiver(id);
        tempVO.setChangeFlag(changeFlag);
        SingleDeleteBpTemplate<WorkConReceiverVO> bpTemplate = new SingleDeleteBpTemplate<WorkConReceiverVO>(mapper);
        bpTemplate.delete(tempVO);
        return id;
    }
}
